home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / AFINGR.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  9KB  |  349 lines

  1. /*
  2. **  AFINGR.C
  3. **
  4. **  Asynchronous FINGER program.
  5. **  Compare to SFINGR and FINGER.
  6. */
  7.  
  8. #include <windows.h>
  9. #include <winsock.h>
  10.  
  11. #include "wil.h"
  12. #include "message.h"
  13. #include "paint.h"
  14. #include "about.h"
  15. #include "str.h"
  16.  
  17.  
  18. #ifdef WIN32
  19. #define USE_INS HINSTANCE
  20. #define USE_PTR PSTR
  21. #else
  22. #define USE_INS HANDLE
  23. #define USE_PTR LPSTR
  24. #endif
  25.  
  26. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  27.  
  28. /* globals */
  29.  
  30. HWND hMainWnd;            /* main window handle */
  31.  
  32. #define BS            8
  33. #define LF           10
  34. #define CR           13
  35. #define MAX_BUF     512
  36. #define MAX_STR      40
  37.  
  38. #define ONE_SEC    1000
  39. #define TEN_SEC   10000
  40.  
  41. #define AFINGR_PORT  79
  42.  
  43. static HMENU hMenu;
  44. static USE_INS hInstance;
  45. static int WinWidth = 8 * NCOLS;
  46. static int WinHeight = 12 * NROWS + 48;
  47. static char Temp[MAX_BUF+8];
  48. static int  InBufLen = 0;
  49. static char InBuffer[MAX_BUF+1];
  50. static char User[MAX_STR] = "";
  51. static char Host[MAX_STR] = "";
  52. static SOCKET Socket = 0;
  53. static ULONG  HostAddr = 0;
  54. static LPSTR  Ptr;
  55. static HCURSOR ArrowCursor;
  56. static HCURSOR WaitCursor;
  57. static int CompletedFlag = FALSE;
  58.  
  59. /* add character to buffer */
  60.  
  61. static void Add2Buffer(char Chr)
  62. {/* add char to input buffer */
  63.  switch(Chr)
  64.    {case BS:
  65.       if(InBufLen>0)
  66.         {/* backup on screen */
  67.          DisplayChar(BS);
  68.          /* remove last char from buffer */
  69.          InBufLen--;
  70.         }
  71.       break;
  72.     default:
  73.       /* display char */
  74.       DisplayChar(Chr);
  75.       /* put into buffer */
  76.       if(InBufLen<MAX_BUF) InBuffer[InBufLen++] = Chr;
  77.       break;
  78.    }
  79. }
  80.  
  81. /* display error message */
  82.  
  83. static void DisplayError(int Code, LPSTR Msg)
  84. {wilAwaitCancel(Socket,hMainWnd);
  85.  DisplayString("ERROR: ");
  86.  if(Msg) DisplayString(Msg);
  87.  if(Code)
  88.    {wilErrorText(Code,(LPSTR)Temp,50);
  89.     DisplayLine((LPSTR)Temp);
  90.    }
  91.  SetCursor(ArrowCursor);
  92. }
  93.  
  94. /* WinMain */
  95.  
  96. #ifdef WIN32
  97. int WINAPI
  98. #else
  99. int PASCAL
  100. #endif
  101. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  102.         USE_PTR szCmdLine,  int nCmdShow)
  103. {WNDCLASS  wc;
  104.  MSG msg;
  105.  BOOL Result;
  106.  if(!hPrevInstance)
  107.    {/* register main window class */
  108.     wc.style = CS_HREDRAW | CS_VREDRAW;
  109.     wc.lpfnWndProc = MainWndProc;
  110.     wc.cbClsExtra = 0;
  111.     wc.cbWndExtra = 0;
  112.     wc.hInstance = hInst;
  113.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  114.     wc.hCursor = NULL;
  115.     wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  116.     wc.lpszMenuName =  "HostMenu";
  117.     wc.lpszClassName = "HostWClass";
  118.     Result = RegisterClass(&wc);
  119.     if(!Result) return FALSE;
  120.    }
  121.  
  122.  /* create main window */
  123.  hInstance = hInst;
  124.  hMainWnd = CreateWindow(
  125.         "HostWClass",   "AFINGR",    WS_OVERLAPPEDWINDOW,
  126.         CW_USEDEFAULT,  CW_USEDEFAULT,
  127.         WinWidth,       WinHeight,
  128.         NULL,           NULL,
  129.         hInstance,      NULL);
  130.  ShowWindow(hMainWnd, nCmdShow);
  131.  UpdateWindow(hMainWnd);
  132.  hMenu = GetMenu(hMainWnd);
  133.  
  134.  /* window control loop */
  135.  
  136.  while(GetMessage(&msg,NULL,0,0))
  137.    {
  138.     TranslateMessage(&msg);
  139.     DispatchMessage(&msg);
  140.    }
  141.  return (msg.wParam);
  142. } /* end WinMain */
  143.  
  144. #ifdef WIN32
  145. LRESULT CALLBACK
  146. #else
  147. long FAR PASCAL
  148. #endif
  149. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  150. {int Code;
  151.  HDC hDC;
  152.  PAINTSTRUCT ps;
  153. #ifdef WIN32
  154. #else
  155.  static FARPROC lpfnAboutDlgProc;
  156. #endif
  157.  hMainWnd = hWindow;
  158.  switch (iMsg)
  159.     {case WM_CREATE:
  160.       /* create cursors */
  161.       ArrowCursor = LoadCursor(NULL, IDC_ARROW);
  162.       WaitCursor = LoadCursor(NULL, IDC_WAIT);
  163.       SetCursor(ArrowCursor);
  164. #ifdef WIN32
  165. #else
  166.        /* create thunk for Win16 */
  167.        lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  168. #endif
  169.       /* initialize paint module */
  170.       PaintInit();
  171.       /* attach WINSOCK */
  172.       DisplayString("Attaching WINSOCK...");
  173.       Code = wilAttach();
  174.       DisplayLine("OK");
  175.       if(Code<0) DisplayError(Code,"wilAttach fails:");
  176.       else
  177.         {wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
  178.          DisplayLine((LPSTR)Temp);
  179.          wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
  180.          DisplayLine((LPSTR)Temp);
  181.          wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
  182.          DisplayLine((LPSTR)Temp);
  183.         }
  184.       break;
  185.  
  186.      case WM_COMMAND:
  187.          switch(wParam)
  188.            {case MSG_ABOUT :
  189. #ifdef WIN32
  190.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  191. #else
  192.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  193. #endif
  194.                return 0;
  195.  
  196.             case MSG_EXIT:
  197.               wilRelease();
  198.               DestroyWindow(hMainWnd);
  199.               break;
  200.  
  201.             case MSG_DEBUG:
  202.               Code = wilDebug(0);
  203.               wsprintf((LPSTR)Temp,"Debug(0) returned %d",Code);
  204.               DisplayLine((LPSTR)Temp);
  205.               break;
  206.             case MSG_BREAK:
  207.               wilCancelBlocking();
  208.               wilAwaitCancel(Socket,hMainWnd);
  209.               break;
  210.  
  211.             case MSG_FINGER:
  212.               InBufLen = 0;
  213.               DisplayString("Enter user (usr@domain):");
  214.               break;
  215.            }
  216.          break;
  217.  
  218.     case WM_PAINT:
  219.       HideCaret(hMainWnd);
  220.       hDC = BeginPaint(hMainWnd, &ps);
  221.       SetMapMode(hDC,MM_ANISOTROPIC);
  222.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  223.       PaintMain(hDC,&ps);
  224.       EndPaint(hMainWnd,&ps);
  225.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  226.       ShowCaret(hMainWnd);
  227.       break;
  228.  
  229.     case WM_DESTROY:
  230.       PostQuitMessage(0);
  231.       break;
  232.  
  233.     case WM_USER:
  234.  
  235. #if 0
  236. wsprintf((LPSTR)Temp,"[%ld]", lParam);
  237. DisplayString((LPSTR)Temp);
  238. #endif
  239.  
  240.       /* received message from wilAwaitEvent */
  241.       switch( LOWORD(lParam) )
  242.         {case FD_CONNECT:
  243.            /* we are now connected. send user name to server */
  244.            wsprintf((LPSTR)Temp,"%s\r\n",(LPSTR)User);
  245.            Code = wilWriteString(Socket,(LPSTR)Temp);
  246.            if(Code<0)
  247.              {DisplayError(Code, NULL);
  248.               break;
  249.              }
  250.            /* await 1st response */
  251.            wilAwaitRead(Socket,hMainWnd);
  252.            break;
  253.  
  254.          case FD_READ:
  255.            if(CompletedFlag) break;
  256.            Code = wilReadLine(Socket,(LPSTR)InBuffer,MAX_BUF);
  257.            if(Code==WIL_EOF)
  258.              {/* we are done */
  259.               wilAwaitCancel(Socket,hMainWnd);
  260.               DisplayLine("<EOF>");
  261.               CompletedFlag = TRUE;
  262.               wilCloseSocket(Socket);
  263.               SetCursor(ArrowCursor);
  264.               break;
  265.              }
  266.            if(Code>0)
  267.              {/* display line from server */
  268.               DisplayString((LPSTR)InBuffer);
  269.               /* await next line */
  270.               break;
  271.              }
  272.            if(Code<0)
  273.              {/* error */
  274.               DisplayError(Code,"wilRead fails:");
  275.               break;
  276.              }
  277.            /* Code == 0 */
  278. #if 0
  279.            DisplayChar('!');
  280. #endif
  281.         }
  282.       break;
  283.  
  284.     case WM_CHAR:
  285.       if(wParam==CR)
  286.         {/* do the CR */
  287.          DisplayChar((char)wParam);
  288.          /* user has completed input */
  289.          DisplayChar(LF);
  290.          InBuffer[InBufLen] = '\0';
  291.          /* execute command */
  292.          wsprintf((LPSTR)Temp,"User@Host: %s",(LPSTR)InBuffer);
  293.          DisplayLine((LPSTR)Temp);
  294.          /* extract user & host names */
  295.          Ptr = StringChar((LPSTR)InBuffer,'@');
  296.          if(Ptr==NULL)
  297.            {DisplayError(Code, "Cannot recognize User@Host");
  298.             break;
  299.            }
  300.          *Ptr = '\0';
  301.          lstrcpy((LPSTR)User, (LPSTR)InBuffer);
  302.          lstrcpy((LPSTR)Host, (LPSTR)(++Ptr));
  303.          wsprintf((LPSTR)Temp,"User ='%s'", (LPSTR)User);
  304.          DisplayLine(Temp);
  305.          wsprintf((LPSTR)Temp,"Host ='%s'", (LPSTR)Host);
  306.          DisplayLine(Temp);
  307.          SetCursor(WaitCursor);
  308.          /* ask for host by name */
  309.          Code = wilAskHostByName((LPSTR)Host);
  310.          if(Code<0)
  311.            {DisplayError(Code, NULL);
  312.             break;
  313.            }
  314.          /* get host address */
  315.          HostAddr = wilGetHostAddr(0);
  316.          if(HostAddr==0)
  317.            {DisplayError(0, "Cannot get IP addess");
  318.             break;
  319.            }
  320.          wsprintf((LPSTR)Temp,"HostAddr = %s\n", wilGetHostDotted(0));
  321.          DisplayLine((LPSTR)Temp);
  322.          /* create socket */
  323.          Socket = wilTcpSocket();
  324.          if((int)Socket<0)
  325.            {DisplayError((int)Code, NULL);
  326.             break;
  327.            }
  328.          CompletedFlag = FALSE;
  329.          /* WM_USER message will be sent when connect is done */
  330.          wilAwaitConnect(Socket,hMainWnd);
  331.          /* initiate connect to remote host */
  332.          Code = wilConnect(Socket,HostAddr,AFINGR_PORT);
  333.          if(Code<0)
  334.            {DisplayError(Code, "Connect:");
  335.             break;
  336.            }
  337.         }
  338.       else
  339.         {/* add char to input buffer */
  340.          Add2Buffer((char)wParam);
  341.         }
  342.       break;
  343.  
  344.     default:
  345.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  346.    }
  347.  return 0;
  348.  
  349. } /* end MainWndProc */